package com.ruoyi.legal.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.ResponseModel;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.legal.domain.vo.SsyhBatchVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.legal.domain.Ssyh;
import com.ruoyi.legal.service.ISsyhService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 三书一函Controller
 *
 * @author xwj
 * @date 2025-04-27
 */
@RestController
@RequestMapping("/legal/ssyh")
@Api(tags = "三书一函")
public class SsyhController extends BaseController {
    @Autowired
    private ISsyhService ssyhService;

    /**
     * 查询三书一函列表
     */
    @PostMapping("/list")
    @ApiOperation("分页查询")
    public TableDataInfo list(@RequestBody Ssyh ssyh) {
        startPage();
        List<Ssyh> list = ssyhService.selectSsyhList(ssyh);
        return getDataTable(list);
    }

    @PostMapping("/listAll")
    @ApiOperation("不分页查询")
    public AjaxResult listAll(@RequestBody Ssyh ssyh) {
        startOrderBy();
        List<Ssyh> list = ssyhService.selectSsyhList(ssyh);
        return success(list);
    }

    /**
     * 导出三书一函列表
     */
    @Log(title = "三书一函", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出")
    public void export(HttpServletResponse response, Ssyh ssyh) {
        List<Ssyh> list = ssyhService.selectSsyhList(ssyh);
        ExcelUtil<Ssyh> util = new ExcelUtil<Ssyh>(Ssyh.class);
        util.exportExcel(response, list, "三书一函数据");
    }

    /**
     * 获取三书一函详细信息
     */
    @GetMapping(value = "/info/{id}")
    @ApiOperation("详情")
    public AjaxResult info(@PathVariable("id") Long id) {
        return success(ssyhService.selectSsyhById(id));
    }

    @PostMapping("/batchSaveUpdate")
    @ApiOperation("批量保存")
    public ResponseModel<SsyhBatchVo> batchSaveUpdate(@RequestBody SsyhBatchVo batchVo) {
        SysUser user = getLoginUser().getUser();
        ssyhService.batchSaveUpdate(batchVo, user);
        return ResponseModel.of(batchVo);
    }
    /**
     * 新增三书一函
     */
    @Log(title = "三书一函", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ApiOperation("新增")
    public AjaxResult save(@RequestBody Ssyh ssyh) {
        SysUser user = getLoginUser().getUser();
        if (ssyh.getId() == null) {
            ssyh.setStatus("0");
            ssyh.setCreateId(user.getUserId());
            ssyh.setCreateTime(DateUtils.getNowDate());
            ssyh.setCreateBy(user.getNickName());
            ssyhService.insertSsyh(ssyh);
        } else {
            ssyh.setUpdateId(user.getUserId());
            ssyh.setUpdateTime(DateUtils.getNowDate());
            ssyh.setUpdateBy(user.getNickName());
            ssyhService.updateSsyh(ssyh);
        }

        return success(ssyh);
    }

    /**
     * 修改三书一函
     */
    @Log(title = "三书一函", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改")
    public AjaxResult update(@RequestBody Ssyh ssyh) {
        SysUser user = getLoginUser().getUser();
        ssyh.setUpdateId(user.getUserId());
        ssyh.setUpdateTime(DateUtils.getNowDate());
        ssyh.setUpdateBy(user.getNickName());
        return success(ssyhService.updateSsyh(ssyh));
    }

    /**
     * 删除三书一函
     */
    @Log(title = "三书一函", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除或批量删除")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ssyhService.deleteSsyhByIds(ids));
    }

    @Log(title = "三书一函", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation("条件删除")
    public ResponseModel delete(@RequestBody Ssyh ssyh) {
        int num = ssyhService.delete(ssyh);
        return ResponseModel.of(num);
    }

    @PostMapping("/queryPage")
    @ApiOperation("分页查询-mp")
    public ResponseModel<Pages<Ssyh>> queryPage(@RequestBody Ssyh ssyh) {
        Pages<Ssyh> page = ssyhService.queryPage(ssyh);
        return ResponseModel.of(page);
    }
}
